- /* sxfxxadd.cpp by K.Tsuru */
- // function ID = 507 BRADIX
- /*****************************************************
- SDecimal class
- Provides result = m + n where m and n have the same sign.
- ******************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- static const char* func = "XXAdd";
- void XXAdd(const SDecimal& m, const SDecimal& n, SDecimal& result)
- {
- int msign = m.Sign(), nsign = n.Sign(), sgn = msign*nsign;
- if(sgn == 0){ result = m.Sign() ? m : n; return; }
- if(sgn < 0) m.SetError(m.SIGN_ERR, func, 507);
- //Three objects must have the same size.
- if( (m.figure.size() != n.figure.size()) || (result.figure.size() != n.figure.size()) )
- m.SetError(m.SYNTAX_ERR, func, 507);
- fType t;
- uint rh = result.Size()-1;
- uint rt = min(m.Tail(), n.Tail());
- fType* rv = result.figure.Elements();
- const fType* mv = m.ReadFigures();
- const fType* nv = n.ReadFigures();
-
- #ifndef NDEBUG
- result.figure(rh); m.figure(rh);
- #endif
- t = 0;
- int i;
- for(i = rh; i >= (int)rt; i--) {
- t += mv[i] + nv[i];
- rv[i] = t & BRADIX1;
- t >>= BRADIX_BITS;
- }
- //Here i = rt -1.
- //When rt == 0 result use up to rv[0].
- if(t){ //It proceeds the carry.
- if(i >= 0){ // rt >= 1
- rv[i] = (fType)t; rt--;
- } else m.SetError(m.OVERFLOW_ERR, func, 507);
- }
- //Clear the outside of the above roop.
- if(result.aHead > rh) result.figure.clear(rh+1, result.aHead);
- if(result.aTail < rt) result.figure.clear(result.aTail, rt-1);
- result.aTail = rt;
- //It decides the figure position.
- while(!rv[rh]) rh--;
- result.aHead = rh;
- result.SetSign(msign); //It must be m+n != 0.
- #ifndef NDEBUG
- result.figure(rt); result.figure(rh);
- #endif
- }
sxfxxadd.cpp : last modifiled at 2017/03/13 14:32:02(1,700 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).